﻿Public Class clsTask
    Public Function SelectTaskStepByTaskId(task_id As String) As DataTable
        Dim szSql As String = ""
        szSql &= "SELECT * FROM agv.task_step where task_id = ? "
        Dim dt As DataTable = DBacs.ExecuteSql(szSql, -1, task_id)
        If IsNothing(dt) Then
            SelectTaskStepByTaskId = New DataTable
        Else
            SelectTaskStepByTaskId = dt
        End If
    End Function

    Public Function task_transfer(id As String) As Integer
        Dim szSql As String = String.Empty
        Dim rtn As Integer
        DBacs.BeginTransaction()
        Try
            szSql &= "select * from agv.tmj where task_step_id =  ?  "
            Dim dtrt As DataTable = DBacs.ExecuteSql(szSql, -1, id)
            szSql = String.Empty
            szSql &= "select * from agv.task_step where sys_id =  ?  "
            Dim dtTaskStep As DataTable = DBacs.ExecuteSql(szSql, -1, id)
            szSql = String.Empty
            szSql &= "select * from agv.task where sys_id =  ?  "
            Dim dtTask As DataTable = DBacs.ExecuteSql(szSql, -1, dtTaskStep.Rows(0)("task_id").ToString())
            Dim en_task_record As New entity_task_record
            en_task_record.sys_id = dtTask.Rows(0)("sys_id")
            en_task_record.type = dtTask.Rows(0)("type")
            en_task_record.prod_id = dtTask.Rows(0)("prod_id")
            en_task_record.status = dtTask.Rows(0)("status")
            If String.IsNullOrEmpty(dtTask.Rows(0)("start_time").ToString()) Then
                en_task_record.start_time = DBNull.Value.ToString()
            Else
                en_task_record.start_time = dtTask.Rows(0)("start_time")
            End If
            If String.IsNullOrEmpty(dtTask.Rows(0)("end_time").ToString()) Then
                en_task_record.end_time = DBNull.Value.ToString()
            Else
                en_task_record.end_time = dtTask.Rows(0)("end_time")
            End If

            en_task_record.update_time = dtTask.Rows(0)("update_time")
            en_task_record.tm = dtTask.Rows(0)("tm")
            en_task_record.lhj = dtTask.Rows(0)("lhj")
            en_task_record.prod_spec_id = dtTask.Rows(0)("prod_spec_id")
            Dim en_task_step_record1 As New entity_task_step_record
            Dim en_task_step_record2 As New entity_task_step_record
            Dim dttstep As DataTable = SelectTaskStepByTaskId(dtTask.Rows(0)("sys_id"))
            For idx = 0 To dttstep.Rows.Count - 1
                If String.IsNullOrEmpty(dttstep.Rows(idx)("previous_id").ToString()) Then
                    en_task_step_record1.sys_id = dttstep.Rows(idx)("sys_id")
                    en_task_step_record1.prio = dttstep.Rows(idx)("prio")
                    en_task_step_record1.pick = dttstep.Rows(idx)("pick")
                    en_task_step_record1.drop = dttstep.Rows(idx)("drop")
                    en_task_step_record1.agv = dttstep.Rows(idx)("agv")
                    en_task_step_record1.task_id = dttstep.Rows(idx)("task_id")
                    en_task_step_record1.status = dttstep.Rows(idx)("status")
                    en_task_step_record1.prod_id = dttstep.Rows(idx)("prod_id")
                    en_task_step_record1.update_time = dttstep.Rows(idx)("update_time")
                    en_task_step_record1.lhj = dttstep.Rows(idx)("lhj")
                    en_task_step_record1.prod_spec_id = dttstep.Rows(idx)("prod_spec_id")
                End If
                If String.IsNullOrEmpty(dttstep.Rows(idx)("previous_id").ToString()) = False Then
                    en_task_step_record2.sys_id = dttstep.Rows(idx)("sys_id")
                    en_task_step_record2.prio = dttstep.Rows(idx)("prio")
                    en_task_step_record2.pick = dttstep.Rows(idx)("pick")
                    en_task_step_record2.drop = dttstep.Rows(idx)("drop")
                    en_task_step_record2.agv = dttstep.Rows(idx)("agv")
                    en_task_step_record2.task_id = dttstep.Rows(idx)("task_id")
                    en_task_step_record2.status = dttstep.Rows(idx)("status")
                    en_task_step_record2.prod_id = dttstep.Rows(idx)("prod_id")
                    en_task_step_record2.previous_id = dttstep.Rows(idx)("previous_id")
                    en_task_step_record2.update_time = dttstep.Rows(idx)("update_time")
                    en_task_step_record2.lhj = dttstep.Rows(idx)("lhj")
                    en_task_step_record2.prod_spec_id = dttstep.Rows(idx)("prod_spec_id")
                End If
            Next
            '更新新任务步骤状态
            szSql = String.Empty
            szSql &= "insert into agv.task_record(sys_id,type,prod_id,status,start_time,end_time,tm,update_time,lhj,prod_spec_id) "
            szSql &= " values (?,?,?,?,?,?,?,?,?,?)"
            rtn = DBacs.ExecuteUpdateSql(szSql, -1， en_task_record.sys_id, en_task_record.type, en_task_record.prod_id, en_task_record.status, en_task_record.start_time, en_task_record.end_time, en_task_record.tm, en_task_record.update_time, en_task_record.lhj, en_task_record.prod_spec_id)

            szSql = String.Empty
            szSql &= "insert into agv.task_step_record(sys_id,prio,pick,drop,agv,task_id,status,prod_id,update_time,lhj,prod_spec_id) "
            szSql &= " values (?,?,?,?,?,?,?,?,?,?,?)"
            rtn = DBacs.ExecuteUpdateSql(szSql, -1， en_task_step_record1.sys_id, en_task_step_record1.prio, en_task_step_record1.pick, en_task_step_record1.drop, en_task_step_record1.agv,
                                                     en_task_step_record1.task_id, en_task_step_record1.status, en_task_step_record1.prod_id, en_task_step_record1.update_time, en_task_step_record1.lhj, en_task_step_record1.prod_spec_id)
            szSql = String.Empty
            szSql &= "insert into agv.task_step_record(sys_id,prio,pick,drop,agv,task_id,status,prod_id,previous_id,update_time,lhj,prod_spec_id) "
            szSql &= " values (?,?,?,?,?,?,?,?,?,?,?,?)"
            rtn = DBacs.ExecuteUpdateSql(szSql, -1， en_task_step_record2.sys_id, en_task_step_record2.prio, en_task_step_record2.pick, en_task_step_record2.drop, en_task_step_record2.agv,
                                                     en_task_step_record2.task_id, en_task_step_record2.status, en_task_step_record2.prod_id, en_task_step_record2.previous_id, en_task_step_record2.update_time, en_task_step_record2.lhj, en_task_step_record2.prod_spec_id)

            szSql = String.Empty
            szSql &= "delete from agv.task where sys_id = ? "
            rtn = DBacs.ExecuteUpdateSql(szSql, -1， en_task_record.sys_id)

            szSql = String.Empty
            szSql &= "delete from agv.task_step where task_id = ? "
            rtn = DBacs.ExecuteUpdateSql(szSql, -1， en_task_record.sys_id)
            DBacs.CommitTransaction()
        Catch ex As Exception
            DBacs.RollbackTransaction()
            rtn = -2
        End Try
        Return rtn
    End Function
End Class
